//
//  UIButton+Extention.swift
//  MicroBlog
//
//  Created by 002 on 15/10/15.
//  Copyright © 2015年 002. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// `便利`构造函数
    convenience init(imageName: String, backImageName: String?) {
        self.init()
        
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        
        if let backImageName = backImageName {
            setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
            setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        }
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:     title
    /// - parameter color:     color
    /// - parameter imageName: 背景图像名称
    ///
    /// - returns: UIButton
    convenience init(title: String, color: UIColor, backImageName: String) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        setBackgroundImage(UIImage.init(named: backImageName), forState: .Normal)
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:     title
    /// - parameter color:     color
    /// - parameter fontSize:  字体大小
    /// - parameter imageName: 图像名称
    /// - parameter backColor: 背景颜色（默认为nil），可以不传入此参数
    ///
    /// - returns: UIButton
    convenience init(title: String, fontSize: CGFloat ,color: UIColor,imageName: String?,backColor : UIColor? = nil) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        
        if let imageName = imageName {
            setImage(UIImage.init(named: imageName), forState: .Normal)
        }
        // 设置背景色
        backgroundColor = backColor
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        sizeToFit()
    }
    
        // 便利构造函数 为什么不行？怎么写
    //    convenience init(imageName: String,backImageName:String) {
    //
    //        let btn = UIButton()
    //        btn.setImage(UIImage(named: imageName), forState: .Normal)
    //        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
    //        btn.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
    //        btn.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
    //
    //        btn.sizeToFit()
    //        
    //        return btn!
    //    }

}